package com.itheima.mapper;

import com.itheima.domain.User;
import com.itheima.utils.MyProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * ClassName:UserMapper
 * Package:com.itheima.mapper
 * Description:
 * Date:2021/11/6 0:01
 * Author:liu.min
 */
public interface UserMapper {

//    private Long id;
//    private String username;
//    private String email;
//    private String password;
//    private String phoneNum;
//    //当前用户具备哪些角色
//    private List<Role> roles;

    @Results({
            //@Result(property = "id", column = "id"),
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "password", column = "password"),
            @Result(property = "phoneNum", column = "phonenum"),
            @Result(
                    property = "roles",
                    column = "id",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.mapper.RoleMapper.findRoleByUserId")
            )
    })
    @Select("select * from sys_user")
    List<User> findAll();

    @Insert("insert into sys_user values(#{id},#{username},#{email},#{password},#{phoneNum})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void save(User user);

    @Insert("insert into sys_user_role values(#{userId},#{roleId})")
    void saveUserRoleRel(@Param("userId") Long id,@Param("roleId") Long roleId);


    @Select("delete from sys_user_role where userId=#{userId}")
    void delUserRoleRel(Long userId);

    @Select("delete from sys_user where id=#{userId}")
    void del(Long userId);

    @Select("select * from sys_user where username=#{username} and password=#{password}")
    User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);


    @Select("select * from sys_user where id=#{id}")
    User findById(int id);

}
